package com.sevenstudio.adu.gasing.state;




import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.util.color.Color;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.define.iServer_Data;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.ARectangle;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASound;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.element.AStrokeText;
import com.sevenstudio.adu.gasing.element.AText;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Menu_Scroll;
import com.sevenstudio.adu.gasing.engine.Utils;

public class State_Select_Rival  {

	GameEngine engine;
	AScene scene;
	ASprite title_rival;
	
	ABackground bg_Select; 
	
	ASprite btn_menu;
	ASprite[][] icon_player = new ASprite[1][3];
	AStrokeText aStrokeText;
	
	Menu_Scroll menu_Scroll;  
	
	
	final float SCALE_MINI = 0.8f;

	float width = 1f;
	
	int INDEX_I, INDEX_J;
	
	private int IND_PATH[][] = {{1,2,3,},{4,5,6}};
	
	boolean Select_player;
	private ARectangle rec;
	
	
	public State_Select_Rival(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
			
		title_rival= new ASprite(engine, iGameDefines.SPRITE_SELECT_PLAYER, iGameDefines.INDEX_TITLE_PILIH_LAWAN);
			
				
		bg_Select = new ABackground(engine, iGameDefines.SPRITE_SELECT_PLAYER, iGameDefines.INDEX_BG_SELECT_RIVAL);

		
		
		title_rival= new ASprite(engine, iGameDefines.SPRITE_SELECT_PLAYER, iGameDefines.INDEX_TITLE_PILIH_LAWAN);
			
				
		bg_Select = new ABackground(engine, iGameDefines.SPRITE_SELECT_PLAYER, iGameDefines.INDEX_BG_SELECT_RIVAL);
		
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		scene.theScene().registerTouchArea(State_Select_Player.btn_cancel.theSprite());
		scene.theScene().registerTouchArea(State_Select_Player.btn_pilih.theSprite());
		
	}

	public void Init() 
	{
		engine.SetGameScene(scene);

		engine.SceneAttach(scene, State_Select_Player.bg_Brown);

		menu_Scroll = new Menu_Scroll(engine, State_Select_Player.sfx_slide.theSound(), 
				State_Main_Activity.CAMERA_WIDTH / 3, State_Select_Player.spr_unlock_player);
		
		
		State_Select_Player.attachUnlockGasing(engine, scene);
		
		engine.SceneAttach(scene, bg_Select);
		
		State_Select_Player.attachIcon(engine, scene);

		
		engine.SceneAttach(scene, State_Select_Player.btn_menu[0]);
		engine.SceneAttach(scene, State_Select_Player.nama_gasing);
		engine.SceneAttach(scene, title_rival);
		
		State_Select_Player.attachPointer(engine, scene);
		State_Select_Player.attachPoint(engine, scene);

	
		State_Select_Player.rec.theRect().setVisible(false);
		engine.SceneAttach(scene, State_Select_Player.rec);
		State_Select_Player.attachAllReview(engine, scene);
		

		engine.SetNextMessageState(engine.NextMessageState());

		
	}
	
	public void Update() 
	{
			
		
		for(int i=0; i < State_Select_Player.spr_unlock_player.length; i++ )
		{
			if(State_Select_Player.spr_unlock_player[i].theSprite().isVisible())
			{
				if(	menu_Scroll.getFocus() == i)
				{

					State_Select_Player.icon_player[i].theSprite().setVisible(true);
					State_Select_Player.nama_gasing.theText().setText(iGameDefines.NAMA_GASING[0][i]);
				
				}
				else 
				{
					State_Select_Player.icon_player[i].theSprite().setVisible(false);
				
				}
			}else {
				State_Select_Player.icon_player[i].theSprite().setVisible(false);
				
			}

		}
		
		if(menu_Scroll.getFocus() < 3)
		{	
			State_Select_Player.hover_pointer[0].theSprite().setVisible(true);
			State_Select_Player.hover_pointer[1].theSprite().setVisible(false);
			State_Select_Player.hover_pointer[2].theSprite().setVisible(false);
		}
		else if(menu_Scroll.getFocus() < 6 && menu_Scroll.getFocus() > 3)
		{
			State_Select_Player.hover_pointer[0].theSprite().setVisible(false);
			State_Select_Player.hover_pointer[1].theSprite().setVisible(true);
			State_Select_Player.hover_pointer[2].theSprite().setVisible(false);
		}
		else if(menu_Scroll.getFocus() < 10 && menu_Scroll.getFocus() > 6)
		{
			State_Select_Player.hover_pointer[0].theSprite().setVisible(false);
			State_Select_Player.hover_pointer[1].theSprite().setVisible(false);
			State_Select_Player.hover_pointer[2].theSprite().setVisible(true);
		}
		
		
		
		
	}

	
	public void exit() 
	{
		
		final EngineLock lock = engine.getEngine().getEngineLock();
			lock.lock();
			
			State_Select_Player.bg_Brown.DetachSelf();
			
			State_Select_Player.detachAllPlayer();
			
			bg_Select.DetachSelf();
			
			State_Select_Player.btn_menu[0].DetachSelf();
			
			
			State_Select_Player.detachIcon();

			State_Select_Player.nama_gasing.theText().detachSelf();
			
			State_Select_Player.nama_gasing.theText().detachSelf();
			
//			btn_info.DetachSelf();
			
			State_Select_Player.rec.theRect().detachSelf();
			State_Select_Player.detachReview();
			
			State_Select_Player.img_point.DetachSelf();
			State_Select_Player.txt_point.theText().detachSelf();
			
			title_rival.DetachSelf();
		
			State_Select_Player.detachPointer();
			
		lock.unlock();
		
	}

	
	public void keyBack(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			
				if(engine.messageCurrentState ==  iGameDefines.MESSAGE_STATE_UPDATE)
				{
					exit();
					engine.SetNextMessageState(0);
				}
				
				engine.SetNextGameState(iGameDefines.GAME_STATE_SELECT_PLAYER);
			
		
		}
	}

	public void SceneTaouch(Scene arg0, TouchEvent arg1)
	{
		if(!State_Select_Player.btn_cancel.theSprite().isVisible())
		{
		menu_Scroll.onTouchEvent(arg1);

		
		if(arg1.isActionDown() && Utils.IsTouched_Down(State_Select_Player.btn_menu[0], arg1))
		{

			State_Select_Player.sfx_Yes.Play();
			State_Select_Player.btn_menu[0].setScale(1f);
			
													
		}
		}
				

		
		//memunculkan review
		
		
		for(int i =0; i < State_Select_Player.spr_unlock_player.length; i++)
		{
			if(arg1.isActionUp() && Utils.IsTouched_Up(State_Select_Player.spr_unlock_player[i], arg1) 
					&& !State_Select_Player.bg_review.theSprite().isVisible()

					&& menu_Scroll.getFocus() == i
					&& State_Select_Player.spr_unlock_player[i].theSprite().isVisible())
			{
//				visibleReview(0, i);

				iServer_Data.INDEX_RIVAL = i;
			}
			

			
		}
		
		
			
		
	}
	
	public void onScrollFinish(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY)
	{
	
		
	}
	
	public void onScroll(ScrollDetector pScollDetector, int pPointerID,
			float pDistanceX, float pDistanceY) 
	{
		
			
	}
	


	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		Utils.TRACE("HIHIHIHI");
		if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(State_Select_Player.btn_cancel.theSprite()) 
				&& State_Select_Player.btn_cancel.theSprite().isVisible())
		{
			
			
		}
		
				
			
		if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(State_Select_Player.btn_pilih.theSprite())
				&& State_Select_Player.btn_pilih.theSprite().isVisible())
		{
		
			engine.SetNextMessageState(engine.NextMessageState());
			
			
		}
	
		
	}

	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY) 
	{
		for(int i=0; i < State_Select_Player.spr_unlock_player.length; i++)
		{
		
			if (Utils.isOnArea(pSceneX, pSceneY, State_Select_Player.spr_unlock_player[i].theSprite()) && menu_Scroll.getFocus() == i)
			{

				State_Select_Player.visibleReview(0, i);
				iServer_Data.INDEX_RIVAL= i;
			}

		}
		if(Utils.isOnArea(pSceneX, pSceneY, State_Select_Player.bg_review.theSprite(), State_Select_Player.btn_cancel.theSprite()) &&
				State_Select_Player.btn_cancel.theSprite().isVisible())
		{
			
			State_Select_Player.notVisibleRiview();
			State_Select_Player.sfx_No.Play();
		}
		
				
		if(Utils.isOnArea(pSceneX, pSceneY, State_Select_Player.bg_review.theSprite(), 
				State_Select_Player.btn_pilih.theSprite()) && State_Select_Player.btn_pilih.theSprite().isVisible())
		{
			
			engine.SetNextMessageState(engine.NextMessageState());
	
			
		}
		
	}
	
	
	
		
	



	

	
	
	

	
}
